﻿using Bmz.IM.Communication;
using Bmz.IM.Model;
using Bmz.Infrastructure.Util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bmz.IM.Client
{
    public partial class LoginForm : BaseForm
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Thread SendThread = new Thread(SendMessage);
            SendThread.Start();
            ClientEngineContext.Current.EventDict.Add(StructMessageType.Login, LoginEvent);
        }


        public void LoginEvent(MessageVO messageVO)
        {
            if (this.InvokeRequired)
            {
                Action<MessageVO> loginToMainFormDelegate = LoginEvent;
                this.Invoke(loginToMainFormDelegate, messageVO);
            }
            else
            {
                if (messageVO.Content == "LoginFail")
                {

                }
                else
                {
                    UserVO user = JsonConvert.DeserializeObject<UserVO>(messageVO.Content);
                    ClientEngineContext.Current.Start(user);
                    this.Hide();
                    MainForm mainForm = new MainForm();
                    mainForm.Text = user.UserName;
                    mainForm.Show();
                }
            }
        }

        private void SendMessage()
        {
            MessageVO messageVO = new MessageVO()
            {
                Type = StructMessageType.Login,
                Content = string.Format("userId:{0}|pwd:{1}|state:{2}", txtUserName.Text, txtPwd.Text, StructUserState.Online),
                FromUserId = txtUserName.Text,
                FromIp = ClientEngineContext.Current.CurrentIPEndPoint.Address.ToString(),
                FromPort = ClientEngineContext.Current.CurrentIPEndPoint.Port,
                ToUserId = "Server",
                ToIp = ConfigurationManager.AppSettings["ServerIp"],
                ToPort = int.Parse(ConfigurationManager.AppSettings["ServerPort"])
            };
            ClientEngineContext.Current.SendMessageUdp(messageVO);
        }

        private void LoginForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Control.ModifierKeys == Keys.Enter)
            {
                e.Handled = true;
                btnLogin.PerformClick();
            }
        }

        private void LoginForm_KeyDown(object sender, KeyEventArgs e)
        {
            if(Keys.Enter==e.KeyCode)
            {
                e.Handled = true;
                btnLogin.PerformClick();
            }
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            System.Environment.Exit(0);
        }
    }
}
